import { ClassConstructor, plainToClass } from "class-transformer";
import { validate } from "class-validator";

export abstract class BaseEntity {
  public async validateThis(skipMissing: boolean = false): Promise<string[]> {
    const errors = await validate(this, {
      skipMissingProperties: skipMissing,
    });
    const temp = errors.map((e) => Object.values(e.constraints as object));
    const result: string[] = [];
    temp.forEach((t) => result.push(...t));
    return result;
  }

  protected static baseTransform<T>(cls: ClassConstructor<T>, plainObject: object): T {
    if (plainObject instanceof cls) {
      return plainObject;
    }
    return plainToClass(cls, plainObject);
  }
}
